package com.ruoyi.web.controller.yiqingDetails;


import com.ruoyi.web.domain.Policy;
import com.ruoyi.web.domain.Region;
import com.ruoyi.web.mapper.PolicyMapper;
import com.ruoyi.web.service.yiqingDetailsService.PolicyService;
import com.ruoyi.web.service.yiqingDetailsService.RegionService;
import com.ruoyi.web.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController()
//在路径前加/yiqingfuwu，避免被ruoyi框架自带的拦截器判断token
//@RequestMapping("/yiqingfuwu")
public class PolicyController {

    @Autowired
    PolicyService policyService;

    @Autowired
    RegionService regionService;

    /**
     * 根据传回的区域id查询指定的疫情政策
     * @param id 疫情政策的id
     * @return
     */
    @GetMapping("/policybyareaid/{areaid}")
    public R getPolicyById(@PathVariable("areaid") Integer id){
        //使用传回的区域id查询数据
        List<Policy> list = policyService.lambdaQuery().eq(Policy::getDictId, id).list();
        //封装数据
        R success = R.ok(list);
        return success;
    }


    /**
     *根据传回的市范围所有的政策
     * @param dictId 城市id
     * @return
     */
    @GetMapping("/policybycityid/{cityId}")
    public R getPolicyByCityId(@PathVariable("cityId") int dictId){

        ArrayList<Policy> policies = new ArrayList<>();

        //获得指定城市id内的所有区
        List<Region> list = regionService.lambdaQuery().eq(Region::getParentId, dictId).list();


        //查询到区中的疫情政策，封装到列表中
        list.stream().forEach(region ->{
//            System.out.println(region);
            //获取每个区域的疫情政策列表
            List<Policy> areaPolicyList = policyService.lambdaQuery().eq(Policy::getDictId, region.getId()).list();

            policies.addAll(areaPolicyList);
        });

        return R.ok(policies);
    }

    /**
     *根据传回的省范围所有的政策
     * @param dictId
     * @return
     */
    @GetMapping("/policybyprovinceid/{dictId}")
    public R getPolicyByProvinceCityId(@PathVariable("dictId") int dictId){

        ArrayList<Policy> policies = new ArrayList<>();

        //获取省中的所有市
        List<Region> cityList = regionService.lambdaQuery().eq(Region::getParentId, dictId).list();

        //获取市中的所有区,并且查询数据加到列表中
        cityList.stream().forEach(city ->{

            //查询市内的所有区
            List<Region> areaList = regionService.lambdaQuery().eq(Region::getParentId, city.getId()).list();
            System.out.println(city);

            //遍历区，查询当前区的所有疫情数据信息，封装到列表中
            areaList.stream().forEach(area ->{
                List<Policy> policyList = policyService.lambdaQuery().eq(Policy::getDictId, area.getId()).list();
                System.out.println(area);
                policies.addAll(policyList);
            });

        });

        return R.ok(policies);
    }





}
